//
//  YHMachineRateMakeItemCell.m
//  YHApp
//
//  Created by -- on 2021/7/24.
//

#import "YHMachineRateMakeItemCell.h"
#import "HAMachineRateMakeMo.h"
#import "PORegexTool.h"

@interface YHMachineRateMakeItemCell ()<UITextFieldDelegate>

/***/
@property (nonatomic, strong) UIView *lineView;
/***/
@property (nonatomic, strong) UILabel *exLabel;
/***/
@property (nonatomic, strong) UIButton *addButton;
/***/
@property (nonatomic, strong) UIButton *minusButton;
/***/
@property (nonatomic, strong) UITextField *numFextField;
/***/
@property (nonatomic, strong) UILabel *titLabel;

@end

@implementation YHMachineRateMakeItemCell

-(void)addButtonClick {
    
    float newValue = [_itemModel.value doubleValue]*1000;
    float max_trade = [_itemModel.max_trade doubleValue]*1000;

    if ([_itemModel.type isEqualToString:@"rate"]) {
        newValue += 10;
        if (newValue > max_trade) {
            [MBProgressHUD showError:@"超过最大值"];
        }else {
            //设置新值
            float valueFloat = (float)(newValue*0.001);
            NSString *newValueString = [NSString stringWithFormat:@"%f",valueFloat];
            newValueString = [NSString changeFloat:newValueString];
            _itemModel.value = newValueString;
            _itemModel.isMake = YES;
            self.numFextField.text = _itemModel.value;
        }
    }
    
    if ([_itemModel.type isEqualToString:@"number"]) {
        newValue += 500;
        if (newValue > max_trade) {
            [MBProgressHUD showError:@"超过最大值"];
        }else {
            //设置新值
            float valueFloat = (float)(newValue*0.001);
            NSString *newValueString = [NSString stringWithFormat:@"%f",valueFloat];
            newValueString = [NSString changeFloat:newValueString];
            _itemModel.value = newValueString;
            _itemModel.isMake = YES;
            self.numFextField.text = _itemModel.value;
        }
    }
}

-(void)minusButtonClick {
    float newValue = [_itemModel.value floatValue]*1000;
    float min_trade = [_itemModel.min_trade floatValue]*1000;

    if ([_itemModel.type isEqualToString:@"rate"]) {
        newValue -= 10;
        if (newValue <  min_trade) {
            [MBProgressHUD showError:@"小于最小值"];
        }else {
            //设置新值
            float valueFloat = (float)(newValue*0.001);
            NSString *newValueString = [NSString stringWithFormat:@"%f",valueFloat];
            newValueString = [NSString changeFloat:newValueString];
            _itemModel.value = newValueString;
            _itemModel.isMake = YES;
            self.numFextField.text = _itemModel.value;
        }
    }
    
    if ([_itemModel.type isEqualToString:@"number"]) {
        newValue -= 500;
        if (newValue < min_trade) {
            [MBProgressHUD showError:@"小于最小值"];
        }else {
            //设置新值
            float valueFloat = (float)(newValue*0.001);
            NSString *newValueString = [NSString stringWithFormat:@"%f",valueFloat];
            newValueString = [NSString changeFloat:newValueString];
            _itemModel.value = newValueString;
            _itemModel.isMake = YES;
            self.numFextField.text = _itemModel.value;
        }
    }
}

-(void)setItemModel:(YHMachineRateMakeItemModel *)itemModel {
    _itemModel = itemModel;

    self.titLabel.text = _itemModel.label;
    self.numFextField.text = _itemModel.value;
    self.exLabel.text = [NSString stringWithFormat:@"设置范围%@~%@",_itemModel.min_trade,_itemModel.max_trade];
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
//    float max_money = [_itemModel.max_trade doubleValue]*1000;
//    float min_money = [_itemModel.min_trade floatValue]*1000;
//    float newValue = [textField.text floatValue]*1000;
//    if (newValue <  min_money) {
//        [MBProgressHUD showError:@"小于最小值"];
//        textField.text = _itemModel.value;
//        return;
//    }
//    if (newValue > max_money) {
//        [MBProgressHUD showError:@"超过最大值"];
//        textField.text = _itemModel.value;
//        return;
//    }
//
//    float valueFloat = (float)(newValue*0.001);
//    NSString *newValueString = [NSString stringWithFormat:@"%f",valueFloat];
//    newValueString = [NSString changeFloat:newValueString];
//    _itemModel.value = newValueString;
    _itemModel.value = textField.text;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if (textField == self.numFextField) {
        
        NSString *textStr = textField.text;
        NSString *t = [NSString stringWithFormat:@"%@%@",textStr,string];
        //所有长度大于6，无视输入
        //        if (t.length > 7) {
        //            return NO;
        //        }
        //限制输入小数点和数字
        BOOL isT1 = [PORegexTool checkInputOnlyPointNumber:t];
        //检测第一个输入的是否是小数点
        if (t.length > 0) {
            BOOL isPoint = [PORegexTool checkInputFristPoint:[t substringToIndex:1]];
            if (isPoint) {
                self.numFextField.text = @"0.";
                return NO;
            }
        }
        //已经包含小数点时，无视输入小数点
        if([textStr rangeOfString:@"."].location != NSNotFound) {
            if([string isEqualToString:@"."]){
                return NO;
            }
        }
        //检测小数点后面是否满2位
        BOOL isT2 = NO;//[PORegexTool checkAfterPointLenth:t];
        //检测
        if (!isT2 && isT1) {
            return YES;
        }else{
            return NO;
        }
    }
    
    return YES;
}


-(void)layoutSubviews {
    [super layoutSubviews];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_offset(0.8);
        make.top.mas_equalTo(54);
    }];
    
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(13);
        make.width.mas_offset(28);
        make.height.mas_offset(28);
    }];
    
    [self.numFextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.addButton.mas_left).offset(-4);
        make.centerY.mas_equalTo(self.addButton);
        make.width.mas_offset(80);
        make.height.mas_offset(28);
    }];
    
    [self.minusButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.numFextField.mas_left).offset(-4);
        make.centerY.mas_equalTo(self.addButton);
        make.width.mas_offset(28);
        make.height.mas_offset(28);
    }];

    [self.titLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerY.mas_equalTo(self.addButton);
        make.right.mas_equalTo(self.minusButton.mas_left).offset(-10);
    }];

    [self.exLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(7);
        make.right.mas_equalTo(-20);
    }];
}

- (UIButton *)addButton
{
    if (!_addButton) {
        _addButton = [[UIButton alloc]init];
        [_addButton setTitle:@"+" forState:UIControlStateNormal];
        [_addButton setTitleColor:[UIColor colorWithHexString:@"#858B9C"] forState:UIControlStateNormal];
        _addButton.titleLabel.font = [UIFont systemFontOfSize:16];
        _addButton.backgroundColor = [UIColor colorWithHexString:@"#F9FAFB"];
        _addButton.layer.cornerRadius = 4;
        _addButton.layer.masksToBounds = YES;
        [_addButton addTarget:self action:@selector(addButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_addButton];
    }
    return _addButton;
}

- (UIButton *)minusButton
{
    if (!_minusButton) {
        _minusButton = [[UIButton alloc]init];
        [_minusButton setTitle:@"-" forState:UIControlStateNormal];
        [_minusButton setTitleColor:[UIColor colorWithHexString:@"#858B9C"] forState:UIControlStateNormal];
        _minusButton.titleLabel.font = [UIFont systemFontOfSize:16];
        _minusButton.backgroundColor = [UIColor colorWithHexString:@"#F9FAFB"];
        _minusButton.layer.cornerRadius = 4;
        _minusButton.layer.masksToBounds = YES;
        [_minusButton addTarget:self action:@selector(minusButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_minusButton];
    }
    return _minusButton;
}

-(UITextField *)numFextField
{
    if(!_numFextField)
    {
        _numFextField = [[UITextField alloc] init];
        _numFextField.textAlignment = NSTextAlignmentCenter;
        _numFextField.font = [UIFont systemFontOfSize:14];
        _numFextField.keyboardType = UIKeyboardTypeDecimalPad;
        _numFextField.delegate = self;
        _numFextField.placeholder = @"";
        _numFextField.backgroundColor = [UIColor colorWithHexString:@"#F9FAFB"];
        _numFextField.layer.cornerRadius = 4;
        _numFextField.layer.masksToBounds = YES;
        _numFextField.backgroundColor = [UIColor colorWithHexString:@"#F9FAFB"];
//        _numFextField.layer.borderColor = [UIColor colorWithHexString:@"#858B9C"].CGColor;
//        _numFextField.layer.borderWidth = 0.5;
//        _numFextField.layer.cornerRadius = 0;
//        _numFextField.layer.masksToBounds = YES;
//        [_numFextField addTarget:self action:@selector(textFieldEditChanged:)forControlEvents:UIControlEventEditingChanged];
        [self.contentView addSubview:_numFextField];
    }
    return _numFextField;
}


- (UILabel *)titLabel
{
    if (!_titLabel) {
        _titLabel = [UILabel ha_NLabelWithFont:18 textColor:@"#000000" text:@"" alignment:0];
        [self.contentView addSubview:_titLabel];
    }
    return _titLabel;
}

- (UIView *)lineView
{
    if (!_lineView) {
        _lineView = [UIView ha_createView];
        _lineView.backgroundColor = [UIColor kLineColor];
        [self.contentView addSubview:_lineView];
    }
    return _lineView;
}

- (UILabel *)exLabel
{
    if (!_exLabel) {
        _exLabel = [UILabel ha_NLabelWithFont:12 textColor:@"#858b9c" text:@"" alignment:0];
        [self.contentView addSubview:_exLabel];
    }
    return _exLabel;
}

@end
